package com.kedacom.ctsp.authz.access;

import com.kedacom.ctsp.authz.Authorize;
import com.kedacom.ctsp.authz.entity.AuthResource;
import com.kedacom.ctsp.authz.entity.Authentication;
import com.kedacom.ctsp.orm.param.Param;
import com.kedacom.ctsp.orm.param.UpdateParam;
import com.kedacom.ctsp.web.aop.context.MethodInterceptorContext;
import com.kedacom.ctsp.web.vo.VO;

import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * DataAccessHandler适配类
 *
 * @author xuwei
 * @create 2017-11-29 13:29
 **/
public abstract class DataAccessHandlerAdaptor implements DataAccessHandler {

    @Override
    public boolean handle(Authentication auth, List<AuthResource> authResources, Authorize anno, MethodInterceptorContext paramContext) {
        Optional<UpdateParam> updateParamOptional = paramContext.getParameter(UpdateParam.class);
        if (updateParamOptional.isPresent()) {
            return handleUpdate(auth, authResources, anno, updateParamOptional.get());
        }
        Optional<Param> paramOptional = paramContext.getParameter(Param.class);
        if (paramOptional.isPresent()) {
            return handleQuery(auth, authResources, anno, paramOptional.get());
        }
        Optional<VO> entityOptional = paramContext.getParameter(VO.class);
        if (entityOptional.isPresent()) {
            return handleVo(auth, authResources, anno, entityOptional.get());
        }
        return true;
    }

    /**
     * insert
     *
     * @param auth
     * @param authResources
     * @param target
     * @return
     */
    protected abstract boolean handleVo(Authentication auth, List<AuthResource> authResources, Authorize anno, VO target);

    /**
     * query
     *
     * @param auth
     * @param authResources
     * @param target
     * @return
     */
    protected abstract boolean handleQuery(Authentication auth, List<AuthResource> authResources, Authorize anno, Param target);

    /**
     * update & delete
     *
     * @param auth
     * @param authResources
     * @param target
     * @return
     */
    protected abstract boolean handleUpdate(Authentication auth, List<AuthResource> authResources, Authorize anno, UpdateParam target);


}
